/**
 * JAVACC DEMO 1.0
 * @copy right zbxsoft company All rights reserved. 
 * @Package com.zbxsoft.platform.template.helper  
 */
package com.apache.design.template.helper;

import com.apache.design.template.FreemarkerHelper;
import com.apache.design.util.DatabaseUtils;
import com.apache.design.util.DesignEntity;
import com.apache.design.util.FileOperate;
import com.apache.design.util.GZipUtil;
import com.apache.info.entity.PubSortInfo;

import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description:  生成属性实现类
 */
public class EntityTempManager extends SupperTemplateManager {

	public boolean doInvoke(DesignEntity entity) {
		boolean mark = false;
		List<PubSortInfo> list = entity.getTabs();
		String jarPath = entity.getPackPath();
		String prefix = entity.getProjectName() + "/src/main/";
		String xmlPath = entity.getFilePath() + "/" + prefix + "java/" + jarPath.replace(".", "/")
				+ "/entity/";
		FileOperate.getInstance().newCreateFolder(xmlPath);
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("packPath", jarPath);
		Connection conn = this.getConn(entity);
		try {
			for (PubSortInfo map : list) {
				data.put("sort", map);
				List mts = DatabaseUtils.getInstance().getTableColumns(conn,map.getInfoTableName());
				data.put("metadatas", mts);
				String result = FreemarkerHelper.instance().getTemplateStr("entity/ObjName.inc", data);
				FileOperate.getInstance().newCreateFile(xmlPath + GZipUtil.toUpperFirst(map.getObjName()) + ".java", result);
			}
			mark = true;
		} catch (Exception e) {
			e.printStackTrace();
			log.error("生成实体失败", e);
		} finally {
			DatabaseUtils.getInstance().close(conn,null);
		}
		return mark;
	}
}
